#include "daq.h"
#include "sys_conf.h"
#include "calc.h"
#include "global.h"

#define NTC_TEMP_SCALE 191
#define NTC_TEMP_OFFSET 39
#define NTC_TEMP_DT 15
//#define K_FACTOR_HI_PRESS 174
#define K_FACTOR_HI_PRESS 124
#define K_FACTOR_HI_ZERO 62
#define K_FACTOR_LO_PRESS 232

#define ACL_TEM_MAX 1400  // 0.1??
#define ACL_TEM_MIN -280

// extern sys_reg_st g_sys;
// 10K
const uint16_t
    ntc_lookup_tab[NTC_TEMP_SCALE] = {193,  204,  215,  227,  239,  252,  265,  279,  294,  309,  324,  340,  357,
                                      375,  393,  411,  431,  451,  //-40℃开始,4096-202/(202+10)*4096
                                      471,  492,  514,  537,  560,  584,  609,  635,  661,  687,  715,  743,  772,
                                      801,  831,  862,  893,  925,  957,  991,  1024, 1058, 1093, 1128, 1164, 1200,
                                      1236, 1273, 1310, 1348, 1386, 1424, 1463, 1501, 1540, 1579, 1618, 1657, 1697,
                                      1736, 1775, 1815, 1854, 1893, 1932, 1971, 2010, 2048, 2087, 2125, 2163, 2200,
                                      2238, 2274, 2311, 2347, 2383, 2418, 2453, 2488, 2522, 2556, 2589, 2622, 2654,
                                      2685, 2717, 2747, 2777, 2807, 2836, 2865, 2893, 2921, 2948, 2974, 3000, 3026,
                                      3051, 3075, 3099, 3123, 3146, 3168, 3190, 3212, 3233, 3253, 3273, 3293, 3312,
                                      3331, 3349, 3367, 3384, 3401, 3418, 3434, 3450, 3465, 3481, 3495, 3510, 3524,
                                      3537, 3551, 3564, 3576, 3588, 3600, 3612, 3624, 3635, 3646, 3656, 3667, 3677,
                                      3686, 3696, 3705, 3714, 3723, 3732, 3740, 3748, 3756, 3764, 3772, 3779, 3786,
                                      3793, 3800, 3807, 3813, 3820, 3826, 3832, 3838, 3844, 3849, 3855, 3860, 3865,
                                      3870, 3875, 3880, 3884, 3889, 3893, 3898, 3902, 3906, 3910, 3914, 3918, 3922,
                                      3925, 3929, 3932, 3936, 3939, 3942, 3945, 3949, 3952, 3954, 3957, 3960, 3963,
                                      3966, 3968, 3971, 3973};  // 150℃结束,4096-0.3104/(0.3104+10)*4096
// 4.7K
// const uint16_t ntc_lookup_tab[NTC_TEMP_SCALE] = {
//			93 ,  98 ,  104 , 110 , 116 , 122 , 129 , 136 , 143 , 151 , 159 , 167 , 176 , 185,  194 , 204 , 214 , 225 ,
//	    236 , 247 , 259 , 271 , 284 , 297 , 311 , 325 , 339 , 354 , 370 , 386 , 403 , 420,  437 , 456 , 474 , 494 ,
//	    513 , 534,  555,  576,  598,  621,  644,  667,  691,  716,  741,  767,  793,  820,  848,  875,  904,  932,
//	    962,  991,  1021, 1052, 1083, 1114, 1146, 1178, 1210, 1243, 1276, 1310, 1343, 1377, 1411, 1445, 1480, 1514,
//	    1549, 1584, 1619, 1654, 1689, 1724, 1759, 1794, 1829, 1864, 1899, 1934, 1968, 2003, 2037, 2071, 2105, 2139,
//	    2173, 2206, 2239, 2272, 2304, 2337, 2368, 2400, 2431, 2462, 2493, 2523, 2553, 2582, 2611, 2640, 2668, 2696,
//	    2723, 2750, 2777, 2803, 2829, 2855, 2880, 2904, 2928, 2952, 2976, 2998, 3021, 3043, 3065, 3086, 3107, 3128,
//	    3148, 3167, 3187, 3206, 3224, 3243, 3261, 3278, 3295, 3312, 3328, 3345, 3360, 3376, 3391, 3406, 3420, 3434,
//	    3448, 3462, 3475, 3488, 3501, 3513, 3525, 3537, 3549, 3560, 3571, 3582, 3593, 3603, 3613, 3623, 3633, 3642,
//	    3652, 3661, 3670, 3678, 3687, 3695, 3703, 3711, 3719, 3727, 3734, 3741, 3748, 3755, 3762, 3769, 3775, 3781,
//	    3788, 3794, 3799, 3805, 3811, 3816, 3822, 3827, 3832, 3837, 3842};
////150℃结束,4096-0.3104/(0.3104+4.7)*4096 100k
const uint16_t ntcX_lookup_tab[NTC_TEMP_SCALE] = {
    //-10℃ - 180℃
    //		  12 ,  13 ,  14 ,  15 ,  16 ,  17 ,  18 ,  19 ,  20 ,  21 ,  23 ,  24 ,  26 ,  27 ,  29 ,  31 ,  33 ,  35 ,
    //	    37 ,  39 ,  42 ,  44 ,  47 ,  49 ,  52 ,  55 ,  58 ,  62 ,  65 ,  69 ,
    73,   77,   81,   85,   90,   94,   99,   105,  110,  116,  122,  128,  134,  141,  148,  155,  163,  170,
    179,  187,  196,  205,  214,  224,  234,  245,  256,  267,  279,  291,  303,  316,  330,  344,  358,  372,
    387,  403,  419,  436,  452,  470,  488,  506,  525,  544,  564,  585,  605,  627,  648,  671,  694,  717,
    741,  765,  789,  815,  840,  866,  893,  920,  947,  975,  1003, 1031, 1060, 1090, 1119, 1149, 1180, 1210,
    1241, 1273, 1304, 1336, 1368, 1400, 1433, 1465, 1498, 1531, 1564, 1597, 1630, 1663, 1697, 1730, 1763, 1797,
    1830, 1863, 1896, 1930, 1963, 1996, 2028, 2061, 2094, 2126, 2158, 2190, 2222, 2253, 2284, 2315, 2346, 2376,
    2407, 2436, 2466, 2495, 2524, 2553, 2581, 2609, 2636, 2664, 2690, 2717, 2743, 2769, 2794, 2819, 2844, 2868,
    2892, 2916, 2939, 2962, 2984, 3006, 3028, 3049, 3070, 3090, 3110, 3130, 3150, 3169, 3188, 3206, 3224, 3242,
    3259, 3276, 3293, 3309, 3325, 3341, 3356, 3372, 3386, 3401, 3415, 3429, 3443, 3456, 3469, 3482, 3494, 3494,
    3507, 3519, 3531, 3542, 3553, 3564, 3575, 3586, 3596, 3606, 3616, 3626, 3635, 3645, 3654, 3663, 3671, 3680,
    3688, 3696, 3704, 3712, 3720, 3727, 3734, 3742, 3749, 3755, 3762};
enum
{
    R22,
    R134A,
    R407C,
    R410A,
    MAXREFRIG
};

const int16_t refrigerant[MAXREFRIG][69] = {
    /****R22*****/
    {-411, -321, -252, -195, -146, -104, -65, -31, 1,   31,  59,  85,  109, 132, 155, 176, 196, 215,
     234,  252,  269,  286,  303,  318,  334, 349, 363, 377, 391, 404, 417, 430, 443, 455, 467, 479,
     490,  502,  513,  524,  534,  545,  555, 565, 575, 585, 595, 604, 614, 623, 632, 641, 650, 658,
     667,  676,  684,  692,  700,  708,  716, 724, 732, 740, 747, 755, 762, 770, 777},  //--0.0-34.0bar (0.5bar step)
    /****R134A*****/
    {-264, -171, -101, -43, 7,   50,  89,  125, 157, 187, 216, 242, 267, 291, 313, 335, 355, 375,
     394,  412,  430,  447, 463, 479, 495, 510, 524, 538, 552, 566, 579, 592, 605, 617, 629, 641,
     652,  664,  675,  686, 696, 707, 717, 727, 737, 747, 757, 766, 776, 785, 794, 803, 812, 820,
     829,  837,  846,  854, 862, 871, 879, 888, 896, 905, 914, 923, 933, 943, 954},
    /****R407C*****/
    {-369, -283, -217, -163, -117, -76, -40, -7,  24,  52,  78,  102, 126, 148, 168, 188, 207, 226,
     243,  260,  276,  292,  307,  322, 336, 350, 364, 377, 390, 402, 414, 426, 438, 449, 460, 471,
     482,  492,  502,  512,  522,  532, 541, 551, 560, 569, 578, 587, 595, 604, 612, 620, 628, 636,
     644,  651,  659,  667,  674,  681, 688, 695, 702, 709, 716, 723, 729, 736, 742},
    /****R410A*****/
    {-505, -431, -370, -318, -273, -234, -199, -168, -139, -112, -86, -63, -40, -19, 1,   20,  38,  56,
     73,   89,   105,  120,  134,  149,  163,  176,  189,  202,  214, 226, 238, 249, 261, 272, 282, 293,
     303,  313,  323,  333,  343,  352,  361,  370,  379,  388,  397, 405, 414, 422, 430, 438, 446, 454,
     461,  469,  476,  484,  491,  498,  505,  512,  519,  526,  533, 539, 546, 552, 559}};

static int16_t calc_ntc(uint16_t adc_value, int16_t adjust, int16_t ntc_n)
{
    int16_t ntc_temp;
    int16_t index;
    uint16_t offset;
    uint16_t NTC_offset;
    uint16_t NTC_DPT;
    const uint16_t *ntctable;

    if (ntc_n >= AI_NTC5)
    {
        adjust = 0;
    }
    //    else
    {
        ntctable   = ntc_lookup_tab;
        NTC_offset = NTC_TEMP_OFFSET;
        NTC_DPT    = NTC_TEMP_DT;
    }

    adc_value = 4096 - adc_value;
    index     = bin_search((uint16_t *)ntctable, (NTC_TEMP_SCALE - 1), adc_value);
    // 		rt_kprintf("index=%d,adc_value=%d,ntc_n=%d,\n",index,adc_value,ntc_n);

    if (index < 0)
    {
        return ABNORMAL_VALUE;
    }
    else
    {
        offset   = (adc_value - ntctable[index - 1]) * 10 / (ntctable[index] - ntctable[index - 1]);
        ntc_temp = (index - NTC_offset) * 10 + offset + adjust - NTC_DPT;
        return ntc_temp;
    }
}

//测量UV光强
static int16_t Calc_UV_ai(uint16_t adc_value, uint16_t k_factor, int16_t cali)
{
    int32_t ret_val = 0;
    // As Vo/Vcc*100 = K*P+10, Vadc*4/Vcc*100 = K*P+10. Vadc = 3.3/4096 * N.
    //(((3.3/4096)*N)*4)/Vcc*100 = K*P ;
    ret_val = (float)adc_value * 330 / 4096 + OFFSET_V + (int16_t)(cali);  //
    if (ret_val <= 20)
    {
        ret_val = 0;
    }
    return (int16_t)ret_val;
}

////中值法滤波
//#define CNT 8
// int16_t AVGfilter2(uint8_t i8Type, int16_t i16Value)
//{
//    int8_t i;
//    static int8_t i8Num[AI_MAX_CNT] = {0};
//    static int16_t i16Value_buf[AI_MAX_CNT][CNT];
//    int sum = 0;

//    if (i8Num[i8Type] < CNT)
//    {
//        i8Num[i8Type]++;
//    }
//    else
//    {
//        i8Num[i8Type] = 0;
//    }
//    i16Value_buf[i8Type][i8Num[i8Type]] = i16Value;
//    bubble_sort(i16Value_buf[i8Type], CNT);
//    for (i = 1; i < CNT - 1; i++)
//    {
//        sum += i16Value_buf[i8Type][i];
//    }
//    sum /= (CNT - 2);
//    return (int16_t)(sum);
//}

void ai_sts_update(void)
{
    uint16_t ain_mask_bitmap;
    uint16_t i;

    ain_mask_bitmap = g_sVariable.gPara.dev_mask.ain;
    //    ain_mask_bitmap             = 0x3FF;

    for (i = AI_NTC1; i < AI_NTC_MAX_CNT; i++)
    {
        g_sVariable.status.u16AI[i] = (((ain_mask_bitmap >> i) & 0x0001) != 0)
                                          ? calc_ntc(ADC1ConvertedValue[i], g_sVariable.gPara.TH.u16NTC_Cali[i], i)
                                          : ABNORMAL_VALUE;
    }

    // UV电压

    g_sVariable.status.u16AI[AI_LEDUV1_V] =
        (((ain_mask_bitmap >> (AI_LEDUV1_V)) & 0x0001) != 0) ? Calc_UV_ai(ADC1ConvertedValue[AI_LEDUV1_V], 0, 0) : 0;

    g_sVariable.status.u16AI[AI_LEDUV2_V] =
        (((ain_mask_bitmap >> (AI_LEDUV2_V)) & 0x0001) != 0) ? Calc_UV_ai(ADC1ConvertedValue[AI_LEDUV2_V], 0, 0) : 0;

    g_sVariable.status.u16AI[AI_LEDUV3_V] =
        (((ain_mask_bitmap >> (AI_LEDUV3_V)) & 0x0001) != 0) ? Calc_UV_ai(ADC1ConvertedValue[AI_LEDUV3_V], 0, 0) : 0;

    return;
}

void daq_gvar_update(void)
{
    ai_sts_update();  // update g_ain_inst
    return;
}
